﻿<Window x:Class="GIMS.GUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
        xmlns:Converters="clr-namespace:GraphSharp.Converters;assembly=GraphSharp.Controls"
        xmlns:localGraphItems="clr-namespace:GIMS.GUI.GraphItems"
        xmlns:local="clr-namespace:GIMS.GUI"
        xmlns:extbehaviour="clr-namespace:WPFExtensions.AttachedBehaviours;assembly=WPFExtensions"
        xmlns:Hierarchical="clr-namespace:GraphSharp.Algorithms.Layout.Simple.Hierarchical;assembly=GraphSharp"
        Title="MainWindow" Height="600" Width="1000" Loaded="Window_Loaded"
        x:Name="window">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />

        <Hierarchical:EfficientSugiyamaLayoutParameters x:Key="LayoutParameters" LayerDistance="60" VertexDistance="60"/>

        <!--Vertex-Controls-->
        <DataTemplate x:Key="vertexTemplate" DataType="{x:Type localGraphItems:CustomVertex}" >
            <StackPanel Orientation="Vertical">
                <Image x:Name="vertexTopDrop" Source="Images\arrow_up.png" Width="90"
                        AllowDrop="True" DragOver="vertexTopDrop_DragOver" Drop="vertexTopDrop_Drop"
                        Visibility="{Binding Path=HasTopDrop, Converter={StaticResource boolToVisibilityConverter}}" />
                <TextBlock Text="{Binding Path=Name}" />
                <Image Source="{Binding Path=PreviewImagePath}" 
                       Width="90" Height="90"/>
                <TextBlock Text="{Binding Path=ImageSize}" />
                <Image x:Name="vertexBottomDrop" Source="Images\arrow_down.png" Width="90"
                       Visibility="{Binding Path=HasBottomDrop, Converter={StaticResource boolToVisibilityConverter}}"
                       AllowDrop="True" DragOver="vertexBottomDrop_DragOver" Drop="vertexBottomDrop_Drop" />
            </StackPanel>
        </DataTemplate>

        <!--Vertex-Appearance-->
        <Style TargetType="{x:Type graphsharp:VertexControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type graphsharp:VertexControl}">
                        <Border x:Name="vertexBorder" BorderBrush="DarkGray" Background="Silver" 
                                BorderThickness="2" CornerRadius="5"
                                Padding="{TemplateBinding Padding}">
                            <StackPanel Orientation="Vertical" Background="Transparent">
                                <StackPanel.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Command="local:MainWindowCommands.ConfigureOperatorCommand"  CommandParameter="{Binding}" />
                                        <MenuItem Command="local:MainWindowCommands.SetPreviewVertexCommand"  CommandParameter="{Binding}" />
                                        <MenuItem Command="local:MainWindowCommands.DeleteOperatorCommand"  CommandParameter="{Binding}" />
                                        <MenuItem Command="local:MainWindowCommands.StartNewEdgeCommand"  CommandParameter="{Binding}" />
                                        <MenuItem Command="local:MainWindowCommands.EndNewEdgeCommand"   CommandParameter="{Binding}" />
                                        <MenuItem Command="local:MainWindowCommands.SetAsParamCommand" CommandParameter="{Binding}"
                                                  Visibility="{Binding Path=HasIsParameter, Converter={StaticResource boolToVisibilityConverter}}"/>
                                        <MenuItem Command="local:MainWindowCommands.UnsetAsParamCommand" CommandParameter="{Binding}"
                                                  Visibility="{Binding Path=HasIsParameter, Converter={StaticResource boolToVisibilityConverter}}"/>
                                    </ContextMenu>
                                </StackPanel.ContextMenu>
                                <ContentPresenter Content="{TemplateBinding Vertex}"
                                    ContentTemplate="{StaticResource vertexTemplate}" />
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="extbehaviour:DragBehaviour.IsDragEnabled" Value="False" />
        </Style>

        <!--Edge-Controls-->
        <DataTemplate x:Key="edgeTemplate" DataType="{x:Type localGraphItems:CustomEdge}">
            <Border>
                <StackPanel Orientation="Horizontal"
                            Visibility="{Binding Path=HasSequence, Converter={StaticResource boolToVisibilityConverter}}">
                    <TextBlock Text="{Binding Path=Sequence}" />
                    <StackPanel Orientation="Vertical">
                        <Button MaxWidth="16" MaxHeight="16" Padding="0" x:Name="EdgeSequenzeUp">
                            <Image Width="16" Height="16" Margin="0" Source="Images/bullet_arrow_up.png" />
                        </Button>
                        <Button Width="16" Height="16" Padding="0" x:Name="EdgeSequenzeDown">
                            <Image Width="16" Height="16" Source="Images/bullet_arrow_down.png" Margin="0" />
                        </Button>
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>

        <!--Edge Appearance-->
        <Converters:EdgeRouteToPathConverter x:Key="routeToPathConverter" />
        <Style TargetType="{x:Type graphsharp:EdgeControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type graphsharp:EdgeControl}">
                        <Canvas>
                            <Path Stroke="{TemplateBinding Foreground}"
                                StrokeThickness="{TemplateBinding StrokeThickness}"
                                MinWidth="1"
                                MinHeight="1"
                                ToolTip="{TemplateBinding ToolTip}"
                                x:Name="edgePath">
                                <Path.Data>
                                    <PathGeometry>
                                        <PathGeometry.Figures>
                                            <MultiBinding Converter="{StaticResource routeToPathConverter}">
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Source.(graphsharp:GraphCanvas.X)" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Source.(graphsharp:GraphCanvas.Y)" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Source.ActualWidth" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Source.ActualHeight" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Target.(graphsharp:GraphCanvas.X)" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Target.(graphsharp:GraphCanvas.Y)" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Target.ActualWidth" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Target.ActualHeight" />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="RoutePoints" />
                                            </MultiBinding>
                                        </PathGeometry.Figures>
                                    </PathGeometry>
                                </Path.Data>
                            </Path>
                            <localGraphItems:CustomEdgeContentPresenter Content="{TemplateBinding Edge}" ContentTemplate="{StaticResource edgeTemplate}" />
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Command="local:MainWindowCommands.DeleteEdgeCommand"  CommandParameter="{Binding}" />
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <Setter Property="graphsharp:GraphElementBehaviour.HighlightTrigger" Value="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" />
            <Setter Property="MinWidth" Value="3" />
            <Setter Property="MinHeight" Value="3" />
            <Setter Property="Background" Value="Red" />
            <Setter Property="Foreground" Value="Silver" />
            <Setter Property="Opacity" Value="0.5" />
            <Setter Property="AllowDrop" Value="True" />
            <EventSetter Event="DragOver" Handler="edge_DragOver" />
            <EventSetter Event="Drop" Handler="edge_Drop" />
        </Style>

    </Window.Resources>

    <Window.CommandBindings>
        <CommandBinding Command="local:MainWindowCommands.CreateSkriptCommand" Executed="CreateSkriptCommand_Executed" CanExecute="CreateSkriptCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.ConfigureOperatorCommand" Executed="ConfigureOperatorCommand_Executed" />
        <CommandBinding Command="local:MainWindowCommands.DeleteOperatorCommand" Executed="DeleteOperatorCommand_Executed" CanExecute="DeleteOperatorCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.DeleteEdgeCommand" Executed="DeleteEdgeCommand_Executed" CanExecute="DeleteEdgeCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.StartNewEdgeCommand" Executed="StartNewEdgeCommand_Executed" CanExecute="StartNewEdgeCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.EndNewEdgeCommand" Executed="EndNewEdgeCommand_Executed" CanExecute="EndNewEdgeCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.SetPreviewVertexCommand" Executed="SetPreviewVertexCommand_Executed" CanExecute="SetPreviewVertexCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.ShowGlobalSettingsCommand" Executed="ShowGlobalSettingsCommand_Executed" />
        <CommandBinding Command="local:MainWindowCommands.OptionsWindow" Executed="OptionsWindow_Executed" />
        <CommandBinding Command="local:MainWindowCommands.Execute" Executed="Execute_Executed" CanExecute="Execute_CanExecute"/>
        <CommandBinding Command="local:MainWindowCommands.NewConvertCommand" Executed="NewConvertCommand_Executed" CanExecute="NewConvertCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.NewMogrifyCommand" Executed="NewMogrifyCommand_Executed" CanExecute="NewMogrifyCommand_CanExecute" />
        <CommandBinding Command="local:MainWindowCommands.SetAsParamCommand" Executed="SetAsParamCommand_Executed" />
        <CommandBinding Command="local:MainWindowCommands.UnsetAsParamCommand" Executed="UnsetAsParamCommand_Executed" CanExecute="UnsetAsParamCommand_CanExecute"/>
        <CommandBinding Command="local:MainWindowCommands.UserGuideCommand" Executed="UserGuideCommand_Executed" />
    </Window.CommandBindings>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="80" />
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--Menu-->
        <Menu Grid.ColumnSpan="3" Grid.Row="0" Height="Auto">
            <MenuItem Header="File">
                <MenuItem Header="New">
                    <MenuItem Command="local:MainWindowCommands.NewConvertCommand" />
                    <MenuItem Command="local:MainWindowCommands.NewMogrifyCommand" />
                </MenuItem>
                <MenuItem Command="local:MainWindowCommands.CreateSkriptCommand" />
                <MenuItem Command="local:MainWindowCommands.Execute" />
                <MenuItem Command="local:MainWindowCommands.OptionsWindow" />
            </MenuItem>
            <MenuItem Header="Help">
                <MenuItem Command="local:MainWindowCommands.UserGuideCommand" />
            </MenuItem>
        </Menu>

        <!--Operations-->
        <Grid Grid.Column="0" Grid.Row="1" Background="#3E78A5">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock Grid.Column="0" Grid.Row="0" Margin="10, 15 10, 10" FontSize="20" FontWeight="Bold"
                       Foreground="White" HorizontalAlignment="Center"
                       Text="{Binding Path=CurrentProgramName}" />

            <ItemsControl Grid.Column="0" Grid.Row="1"
                          ItemsSource="{Binding Path=Operators}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Image  Width="59" Height="59" Margin="15" MouseMove="Operation_MouseMove"
                            Source="{Binding Path=IconPath}" Tag="{Binding Path=OperatorName}">
                            <Image.ToolTip>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="{Binding Path=OperatorName}" FontWeight="Bold" />
                                    <TextBlock Text="{Binding Path=OperatorDescription}" />
                                </StackPanel>
                            </Image.ToolTip>
                        </Image>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>

        <!--Graph-->
        <Grid Grid.Column="1" Grid.Row="1" Background="#ff656565">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" MinHeight="50" />
                <RowDefinition Height="18*" />
            </Grid.RowDefinitions>

            <Button Grid.Column="0" Grid.Row="0" Margin="10" FontSize="12" FontWeight="Bold" Command="local:MainWindowCommands.ShowGlobalSettingsCommand">Global Settings</Button>

            <ScrollViewer x:Name="graphScrollViewer" Grid.Column="0" Grid.Row="1" Drop="graph_Drop" AllowDrop="True" DragOver="graph_DragOver">
                <localGraphItems:CustomGraphLayout x:Name="graphLayout" Margin="10" MinWidth="100" MinHeight="100" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    Graph="{Binding Path=Graph}"
                    LayoutParameters="{StaticResource ResourceKey=LayoutParameters}"
                    LayoutAlgorithmType="{Binding Path=LayoutAlgorithmType, Mode=OneWay}"
                    OverlapRemovalAlgorithmType="FSA">
                </localGraphItems:CustomGraphLayout>
            </ScrollViewer>
        </Grid>

        <Expander  Grid.Column="2" Grid.Row="1" HorizontalAlignment="Right" Header="Preview" VerticalAlignment="Stretch"
                  ExpandDirection="Right" MaxWidth="400" VerticalContentAlignment="Center">
            <Image Source="{Binding Path=CurrentVertexForPreview.PreviewImagePath, Mode=OneWay}" StretchDirection="DownOnly" HorizontalAlignment="Left" Margin="10,0,10,0"/>
        </Expander>
    </Grid>
</Window>
